﻿using System;
using System.Collections.Generic;
using System.Text;
using BCCommon;
using BCEntity.OA.TeamWork.TeamWorkCategory;
using BCData.OA.TeamWork.TeamWorkCategory;
using BCDto.OA.TeamWork.TeamWorkCategory;
using BCService.Utils;

namespace BCService.OA.TeamWork.TeamWorkCategory
{
    public class TeamWorkCategoryService : ITeamWorkCategoryService
    {
        private readonly IApplicationContextService applicationContextService;
        private readonly ITeamWorkCategoryData teamWorkCategoryData;

        public TeamWorkCategoryService(IApplicationContextService applicationContextService,
            ITeamWorkCategoryData teamWorkCategoryData)
        {
            this.applicationContextService = applicationContextService;
            this.teamWorkCategoryData = teamWorkCategoryData;
        }

        public TeamWorkCategoryDto Add(TeamWorkCategoryRequestDto requestDto)
        {
            if (this.teamWorkCategoryData.Exists(requestDto.TeamWorkCategoryCode.Trim()).GetAwaiter().GetResult())
            {
                throw new ArgumentException("协同办公分类编码已存在");
            }
            var entity = requestDto.As<TeamWorkCategoryEntity>();
            entity.CreateAdminName = applicationContextService.Admin.FullName;
            entity.CreateAdminId = applicationContextService.Admin.AdminId;

            return this.teamWorkCategoryData.Add(entity).GetAwaiter().GetResult().As<TeamWorkCategoryDto>();
        }
        public TeamWorkCategoryDto Update(int teamWorkCategoryId, TeamWorkCategoryRequestDto requestDto)
        {
            var oldEntity = this.teamWorkCategoryData.GetEntity(teamWorkCategoryId).GetAwaiter().GetResult();

            if (oldEntity == null)
            {
                throw new ArgumentException("此记录不存在");
            }

            if (oldEntity.TeamWorkCategoryCode != requestDto.TeamWorkCategoryCode)
            {
                if (this.teamWorkCategoryData.Exists(requestDto.TeamWorkCategoryCode.Trim()).GetAwaiter().GetResult())
                {
                    throw new ArgumentException("协同办公分类编码已存在");
                }
            }
            if (oldEntity.IsPredefined)
            {
                throw new ArgumentException("预定义协同办公分类不允许修改");
            }

            oldEntity.TeamWorkCategoryCode = requestDto.TeamWorkCategoryCode;
            oldEntity.TeamWorkCategoryName = requestDto.TeamWorkCategoryName;
            oldEntity.EditAdminName = applicationContextService.Admin.FullName;
            oldEntity.EditAdminId = applicationContextService.Admin.AdminId;

            return this.teamWorkCategoryData.Update(oldEntity).GetAwaiter().GetResult().As<TeamWorkCategoryDto>();
        }

        public bool Delete(int teamWorkCategoryId)
        {
            return this.teamWorkCategoryData.Delete(teamWorkCategoryId).GetAwaiter().GetResult();
        }

        public TeamWorkCategoryDto GetEntity(int teamWorkCategoryId)
        {
            return this.teamWorkCategoryData.GetEntity(teamWorkCategoryId).GetAwaiter().GetResult().As<TeamWorkCategoryDto>();
        }

        public IEnumerable<TeamWorkCategoryDto> GetList()
        {
            return this.teamWorkCategoryData.GetList().GetAwaiter().GetResult().As<IEnumerable<TeamWorkCategoryDto>>();
        }

        public PageableList<TeamWorkCategoryDto> Query(string teamWorkCategoryName, string teamWorkCategoryCode, DateTime? start, DateTime? end, int pageIndex, int pageSize)
        {
            var (list, count) = this.teamWorkCategoryData.Query(teamWorkCategoryName, teamWorkCategoryCode, start, end, pageIndex, pageSize).GetAwaiter().GetResult();

            return new PageableList<TeamWorkCategoryDto>
            {
                Items = list.As<IEnumerable<TeamWorkCategoryDto>>(),
                Count = count,
                PageIndex = pageIndex,
                PageSize = pageSize
            };
        }
    }
}
